package com.atguigu.test;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

import java.io.IOException;

public class test_es_01_index_create {

    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));

        System.out.println("es连接是否成功: "+esClient.ping(RequestOptions.DEFAULT));

        //创建索引请求对象
        CreateIndexRequest request = new CreateIndexRequest("user");
        
        //发送请求、获取响应
        CreateIndexResponse response = esClient.indices().create(request,RequestOptions.DEFAULT);
        
        //响应状态
        boolean acknowledged = response.isAcknowledged();
        System.out.println("响应状态: "+ acknowledged);

        //关闭es客户端
        esClient.close();
    }
}
